//
//  main.m
//  05_枚举
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 LST. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    
    // 枚举器 (遍历)
    // 数组
    // NSEnumerator依赖于集合类型, 不能单独使用
    NSArray *StringArray = @[@"0.0", @"x.x", @"@.@", @"123", @"456"];
    NSEnumerator *stringArrayEnumerator = [StringArray objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnumerator nextObject]) != nil) {
        NSLog(@"value = %@", value);
    }
    // 倒序枚举
    NSEnumerator *rever = [StringArray reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [rever nextObject]) {
        NSLog(@"value = %@", value1);
    }
    // 字典枚举 (字典无序 ,没有倒序 reverseObjectEnumerator)
    // 默认取出来的值, 是Value (取key的话 用.allKeys)
    NSDictionary *dic = @{@"name" : @"压力三大", @"sex" : @"男"};
    NSEnumerator *dicEnumerator = [dic objectEnumerator];
    id valueDic = nil;
    while (valueDic = [dicEnumerator nextObject]) {
        //   NSArray *arrkey = [dic allKeysForObject:valueDic];
        NSLog(@"%@", valueDic);
    }
    // 集合枚举 (无序 没有倒序)
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"119", @"114", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"%@", setValue);
    }
    // 快速枚举 for...in (是对枚举的一层封装)
    for (NSString *string in StringArray) {
        NSLog(@"%@", string);
    }
    NSMutableString *mutStr = [[NSMutableString alloc] init];
    for (NSString *newString in StringArray) {
        [mutStr appendString:newString];
    }
    NSLog(@"%@", mutStr);
    // forin 字典
    for (NSString *key in dic) {
        NSLog(@"%@", key);
        NSLog(@"%@", [dic objectForKey:key]);
    }
    // forin 集合
    for (NSString *anyObject in set) {
        NSLog(@"anyObject = %@", anyObject);
    }
    // 排序
    // 参数1. key:排序条件
    // 参数2. YES(升序)or NO(降序)
    NSArray *array = @[@1, @2, @3, @4];
    
    NSSortDescriptor *sortDescripitor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    // 数组有排序的方法(sortedArrayUsingDescriptors)
    // 参数为一个排序条件的数组
    array = [array sortedArrayUsingDescriptors:@[sortDescripitor]];
    NSLog(@"%@", array);
    // 可变数组
    NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@10, @11, @12, @13, nil];
    [mutableArray sortUsingDescriptors:@[sortDescripitor]];
    NSLog(@"%@", mutableArray);
    // 排序1: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序直接可以在本身之上修改
    Person *person1 = [[Person alloc] init];
    person1.name = @"小一";
    person1.age = 18;
    Person *person2 = [[Person alloc] init];
    person2.name = @"小二";
    person2.age = 19;
    Person *person3 = [[Person alloc] init];
    person3.name = @"小三";
    person3.age = 20;
    Person *person4 = [[Person alloc] init];
    person4.name = @"小四";
    person4.age = 21;
    Person *person5 = [[Person alloc] init];
    person5.name = @"小五";
    person5.age = 22;
    // NSSortDescriptor排序
    NSArray *perArray = @[person1, person2, person3, person4, person5];
    NSLog(@"%@", perArray);
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"agec" ascending:NO];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@", perArray);
    
    // 排序2 sortedArrayUsingSelector @selector: 方法
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTunes", @"iMac", @"Mac Mini"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
    
    
    
    return 0;
}
